<template>
  <div>
    <el-dialog
      title="编辑会员"
      :visible.sync="editMemberDialog"
      width="35%"
      @close="close"
    >
      <el-form ref="form" :rules="rules" :model="editForm" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editForm.mobile" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="editForm.password"
            :type="passwordVisible ? 'text' : 'password'"
            placeholder="如不修改请留空"
          >
            <template slot="suffix">
              <span
                class="el-icon-view"
                @click="togglePasswordVisibility()"
              ></span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="">
          <el-row :gutter="10" style="margin-left: 135px">
            <el-button type="primary" size="small" @click="btnSave"
              >保存</el-button
            >
            <el-button size="small" @click="close">取消</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { editMemberDetailApi } from "@/api/client";
export default {
  props: {
    editMemberDialog: {
      type: Boolean,
    },
  },
  data() {
    return {
      passwordVisible: false,
      editForm: {
        username: "",
        mobile: "",
        password: "",
      },
      rules: {
        mobile: [
          { required: true, messgae: "请输入手机号", trigger: "blur" },
          {
            pattern: /^[1][3456789][0-9]{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        uid: [{ required: true, message: "请输入会员ID", trigger: "blur" }],
        username: [
          { required: true, messgae: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, messgae: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    close() {
      this.$emit("update:editMemberDialog", false);
    },
    togglePasswordVisibility() {
      this.passwordVisible = !this.passwordVisible;
    },
    // 保存
    btnSave() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (valid) {
            console.log(this.editForm);
            const res = await editMemberDetailApi(this.editForm);
            console.log(res);
            if (res.code === 1) {
              this.$emit("update:editMemberDialog");
              this.$parent.memberList()
              this.$message.success(res.data)
            }
          } else {
            this.$message.error("保存失败");
          }
        } catch {
          this.$message.error('保存失败')
        }
      });
    },
  },
};
</script>

<style scoped></style>
